<template>
  <div class="login-container">
    <div class="left-container">
      <img :src="require('@/assets/login.png')" alt="Login">
    </div>
    <div class="right-container">
      <el-form :model="form" class="login-form" :rules="rules" ref="formRef">
        <h3 class="title">融创后台管理系统</h3>
        <el-form-item prop="mobile">
          <span>登陆账号</span>
          <el-input v-model="form.mobile" :prefix-icon="User" placeholder="请输入账号"/>
        </el-form-item>
        <el-form-item prop="password">
          <span>密码</span>
          <el-input v-model="form.password" :prefix-icon="Lock" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import axiosUtil from '@/util/axios'
import {ElMessage} from "element-plus";
import router from "@/router"
import {User, Lock} from '@element-plus/icons-vue'

const form = ref({
  mobile: '',
  password: ''
})

const rules = ref({
  mobile: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
})

const formRef = ref()

const handleLogin = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        let result = await axiosUtil.post('/app/login', form.value)
        if (result.status === 200) {
          let data = result.data
          window.sessionStorage.setItem('token', data.token)
          window.sessionStorage.setItem('username', data.username)
          await router.replace('/');
        }else {
          ElMessage.error('登录失败')
        }
      }catch (e) {
        ElMessage.error('登录失败')
      }
    }else {
      ElMessage.error('登录失败')
    }
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  background: #ffffff;
  display: flex;
  height: 100vh;

  .left-container {
    height: 100%;
    width: 50%;
    background: yellow;

    img {
      height: 100%;
      width: 100%;
    }
  }

  .right-container {
    height: 100%;
    width: 50%;

    .login-form {
      text-align: center;
      width: 400px;
      margin: 0 auto;
      padding: 160px 0 0 0;

      .title {
        font-size: 28px;
        margin-bottom: 40px;
      }

      .el-form-item {
        margin: 0 0 28px 0;
        span {
          font-size: 16px;
        }

        .el-input {
          height: 40px;
          font-size: 14px;
          padding: 0;
        }
      }

      .login-button {
        width: 100%;
        height: 40px;
        line-height: 20px;
        border-radius: 4px;
        background-color: rgba(199,22,29,1);
        color: rgba(255,255,255,1);
        font-size: 14px;
        text-align: center;
        border: none;
        margin-top: 8px;
      }
    }
  }
}
</style>

